package com.h117121.fashionshop.core.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.h117121.fashionshop.constant.AppConstant;
import com.h117121.fashionshop.core.dao.AccountDao;
import com.h117121.fashionshop.core.exception.AccountNotFoundException;

@Transactional
@Service("accountRestService")
public class AccountRestServiceImpl implements AccountRestService {

	@Autowired
	AccountDao accountDao;

	@Override
	public CheckUsernameResult checkUsername(String username) {
		if (username == null || username.isEmpty()) {
			return CheckUsernameResult.TOOSHORT;
		} else {
			if (username.length() < AppConstant.MINIMUM_CHARACTER_USERNAME) {
				return CheckUsernameResult.TOOSHORT;
			} else {
				if (!StringUtils
						.containsOnly(username,
								"abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ0123456789")) {
					return CheckUsernameResult.INVALID;
				}
			}
		}

		try {
			accountDao.findByUsername(username);
			return CheckUsernameResult.EXISTED;
		} catch (AccountNotFoundException ex) {
			return CheckUsernameResult.VALID;
		}
	}

}
